package de.derhess.projects.pkr.component
{
	import de.derhess.projects.pkr.models.PKROutOfCourt;
	
	import mx.events.PropertyChangeEvent;
	
	import spark.components.SkinnableContainer;
	
	
	/**
	 * Your class description
	 * 
	 * 
	 * @author Florian Weil [derhess.de, Deutschland]
	 * @see http://blog.derhess.de
	 * 
	 */
	public class OutOfCourtContainer extends SkinnableContainer
	{
		//--------------------------------------------------------------------------
		//
		//  Class variables
		//
		//--------------------------------------------------------------------------
		private static const DEFAULT_HEIGHT:Number = 200;
		private static const DEFAULT_WIDTH:Number = 200;
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		
		//--------------------------------------------------------------------------
		//
		//  Initialization
		//
		//--------------------------------------------------------------------------
		public function OutOfCourtContainer()
		{
			
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
		}
		
		
		
		
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		private var _dataModel:PKROutOfCourt;
		private var dataModelChanged:Boolean = false;
		/**
		 * This is dataModel
		 */
		public function get dataModel():PKROutOfCourt
		{
			return _dataModel;
		}	
		/*
		* @private
		*/
		public function set dataModel(value:PKROutOfCourt):void
		{
			if(value && _dataModel != value) {
				
				// removeListener from the old data object and add the listener to the new one
				if(_dataModel) {
					this.dataModel.businessFee.removeEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewVAT);
					this.dataModel.riseFee.removeEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewVAT);
					this.dataModel.conciliationFee.removeEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewVAT);
					this.dataModel.expenses.removeEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewVAT);
					
					this.dataModel.vat.removeEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewSum);
				}
				
				_dataModel = value;
				this.dataModel.businessFee.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewVAT);
				this.dataModel.riseFee.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewVAT);
				this.dataModel.conciliationFee.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewVAT);
				this.dataModel.expenses.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewVAT);
				
				this.dataModel.vat.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewSum);
				dataModelChanged = true;
				invalidateProperties();
			}
		}
		
		

		private var lawyerFeeChanged:Boolean = false;
		private var _lawyerFee:Number = 0;
		/**
		 * This is lawyerFee
		 */
		public function get lawyerFee():Number
		{
			return _lawyerFee;
		}
		
		/*
		* @private
		*/
		public function set lawyerFee(value:Number):void
		{
			if(_lawyerFee != value) {
				lawyerFeeChanged = true;
				_lawyerFee = value;
				invalidateProperties();
			}
		}
		
		
		
		private var numberClientsChanged:Boolean = false;
		private var _numberClients:int = 0;
		/**
		 * This is numberClients
		 */
		public function get numberClients():int
		{
			return _numberClients;
		}
		
		/*
		* @private
		*/
		public function set numberClients(value:int):void
		{
			if(_numberClients != value) {
				numberClientsChanged = true;
				_numberClients = value;
				invalidateProperties();
			}
		}

		
		private var rateVATChanged:Boolean = false;
		private var _rateVAT:Number = 0;
		/**
		 * This is rateVAT
		 */
		public function get rateVAT():Number
		{
			return _rateVAT;
		}
		
		/*
		* @private
		*/
		public function set rateVAT(value:Number):void
		{
			if(_rateVAT != value) {
				rateVATChanged = true;
				_rateVAT = value;
				invalidateProperties();
			}
		}
		
		private var expensesChanged:Boolean = false;
		private var _expenses:Number = 0;
		/**
		 * This is expenses
		 */
		public function get expenses():Number
		{
			return _expenses;
		}
		
		/*
		* @private
		*/
		public function set expenses(value:Number):void
		{
			if(_expenses != value) {
				expensesChanged = true;
				_expenses = value;
				invalidateProperties();
			}
		}
		
		private var conciliationFeeFactorChanged:Boolean = false;
		private var _conciliationFeeFactor:Number = 0;
		/**
		 * This is conciliationFeeFactor
		 */
		public function get conciliationFeeFactor():Number
		{
			return _conciliationFeeFactor;
		}
		
		/*
		* @private
		*/
		public function set conciliationFeeFactor(value:Number):void
		{
			if(_conciliationFeeFactor != value) {
				conciliationFeeFactorChanged = true;
				_conciliationFeeFactor = value;
				invalidateProperties();
			}
		}
		
		private var businessFeeFactorChanged:Boolean = false;
		private var _businessFeeFactor:Number = 0;
		/**
		 * This is businessFeeFactor
		 */
		public function get businessFeeFactor():Number
		{
			return _businessFeeFactor;
		}
		
		/*
		* @private
		*/
		public function set businessFeeFactor(value:Number):void
		{
			if(_businessFeeFactor != value) {
				businessFeeFactorChanged = true;
				_businessFeeFactor = value;
				invalidateProperties();
			}
		}
		//--------------------------------------------------------------------------
		//
		//  Additional getters and setters
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		override protected function measure():void
		{
			super.measure();
			measuredMinHeight = measuredHeight = DEFAULT_HEIGHT;
			measuredMinWidth = measuredWidth = DEFAULT_WIDTH;
		}
		
		override protected function commitProperties():void 
		{
			super.commitProperties();
			
			if(dataModelChanged) {
				this.dataModel.businessFee.baseValue = lawyerFee;
				this.dataModel.riseFee.baseValue = lawyerFee;
				this.dataModel.conciliationFee.baseValue = lawyerFee;
				this.dataModel.riseFee.factor = Math.min((numberClients - 1)*(0.3),2.0);
				this.dataModel.businessFee.factor = businessFeeFactor;
				this.dataModel.conciliationFee.factor = conciliationFeeFactor;
				this.dataModel.expenses.sum = expenses;
				this.dataModel.vat.percentage = rateVAT;
				dataModelChanged = false;
			}
			
			
			if(lawyerFeeChanged && this.dataModel) {
				this.dataModel.businessFee.baseValue = lawyerFee;
				this.dataModel.riseFee.baseValue = lawyerFee;
				this.dataModel.conciliationFee.baseValue = lawyerFee;
				lawyerFeeChanged = false;
			}
			
			if(numberClientsChanged && this.dataModel) {
				this.dataModel.riseFee.factor = Math.min((numberClients - 1)*(0.3),2.0);
				numberClientsChanged = false;
			}
			
			if(businessFeeFactorChanged && this.dataModel) {
				this.dataModel.businessFee.factor = businessFeeFactor;
				businessFeeFactorChanged = false;
			}
			
			if(conciliationFeeFactorChanged && this.dataModel) {
				this.dataModel.conciliationFee.factor = conciliationFeeFactor;
				conciliationFeeFactorChanged = false;
			}
			
			if(expensesChanged && this.dataModel) {
				this.dataModel.expenses.sum = expenses;
				expensesChanged = false;
			}
			
			if(rateVATChanged && this.dataModel) {
				this.dataModel.vat.percentage = rateVAT;
				rateVATChanged = false;
			}
		}
		
		override protected function updateDisplayList(unscaledWidth:Number,unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
		}
		
		override protected function getCurrentSkinState():String {
			return currentState;
		}
		
		override protected function partAdded(partName:String,instance:Object):void
		{
			
		}
		
		override protected function partRemoved(partName:String,instance:Object):void
		{
			
		}
		
		
		//--------------------------------------------------------------------------
		//
		//  API
		//
		//--------------------------------------------------------------------------
		
		
		//--------------------------------------------------------------------------
		//
		//  Eventhandling
		//
		//--------------------------------------------------------------------------
		private function handleNewVAT(e:PropertyChangeEvent):void
		{
			
			var tSum:Number = dataModel.businessFee.sum + dataModel.riseFee.sum + dataModel.conciliationFee.sum + dataModel.expenses.sum;
			this.dataModel.vat.baseValue = tSum;
		}
		
		
		private function handleNewSum(e:PropertyChangeEvent):void
		{
			this.dataModel.sum = dataModel.vat.sum + dataModel.vat.baseValue;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Broadcasting
		//
		//--------------------------------------------------------------------------
		
		
	}
}
